package com.lkm.market2_0.steelyards;

import android.os.Looper;

import com.lkm.comlib.Config;
import com.lkm.frame.task.ProgressData;
import com.lkm.frame.task2.InterceptorAdapter;
import com.lkm.frame.task2.Task;
import com.lkm.hardware_connect.AbsOnDeviceStateEvent;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.market2_0.MyApplication;
import com.lkm.market2_0.m.imple.steelyardserver.SteelyardServerModuleBase;
import com.lkm.serialport_com.connect.SerialportConnectModule;
import com.lkm.serialport_common_com.connect.SerialportConfig;

public class SerialportCom extends SerialportConnectModule {


    private SteelyardServerModuleBase mSteelyardServerModuleBase;

    public SerialportCom(SteelyardServerModuleBase mSteelyardServerModuleBase) {
        super(MyApplication.getInstance().getTaskManager2(), Looper.getMainLooper(), Config.getTaskExecutor());
        this.mSteelyardServerModuleBase = mSteelyardServerModuleBase;
    }

    @Override
    protected SerialportConfig configSerialportConfig() {
        SerialportConfig serialportConfig = new SerialportConfig();
        serialportConfig.setDevName("/dev/ttyS4");
        serialportConfig.setBaud(115200);
        return serialportConfig;
    }

    @Override
    protected AbsOnDeviceStateEvent createOnDeviceStateEvent(int state, int reConnectCount, boolean isError, String errorLog) {
        return new OnSteelyardBluetoothStateEvent(state,reConnectCount,isError,errorLog);
    }

    @Override
    protected void onRead(ProgressData progressData) {

    }

    @Override
    protected Task<IHardwareControler, ?, ?> createReadTask(IHardwareControler hardwareControler) {
         Task<IHardwareControler, ?, ?>  task = mSteelyardServerModuleBase.createReadTask("/dev/ttyS4",hardwareControler);
        task.addInterceptor(new InterceptorAdapter(){

            @Override
            public boolean interceptCancel(Task task) {
                interceptEnd();
                return super.interceptCancel(task);
            }

            @Override
            public boolean interceptComplete(Task task, Object result) {
                interceptEnd();
                return super.interceptComplete(task, result);
            }

            public void interceptEnd() {
//                disConnectForError("结束接待");
            }
        });

        return task;


    }
}
